/* p10_2.c: Stepper motor control */
/* This program controls a unipolar stepper motor using PB 3, 2, 1, 0. */

#include "TM4C123GH6PM.h"

void delayMs(int n);

int delay = 10;
int direction = 0;

int main(void)
{
    const char steps[ ] = {0x9, 0x3, 0x6, 0xC};
    int i = 0;

    SYSCTL->RCGCGPIO |= 0x02;   /* enable clock to GPIOB */
    
    /* PORTB 3, 2, 1, 0 for motor control */
    GPIOB->DIR |= 0x0F;         /* PORTB 3, 2, 1, 0 as output */
    GPIOB->DEN |= 0x0F;         /* PORTB 3, 2, 1, 0 as digital pins */

    for (;;)
    {
        if (direction)
            GPIOB->DATA = (steps[i++ & 3]);
        else
            GPIOB->DATA = (steps[i-- & 3]);
        delayMs(delay);
    }
}

/* delay n milliseconds (16 MHz CPU clock) */
void delayMs(int n)
{
    int i, j;
    for(i = 0 ; i < n; i++)
        for(j = 0; j < 3180; j++)
            {} /* do nothing for 1 ms */
}

/* This function is called by the startup assembly code to perform system specific initialization tasks. */
void SystemInit(void)
{
    /* Grant coprocessor access */
    /* This is required since TM4C123G has a floating point coprocessor */
    SCB->CPACR |= 0x00f00000;
}